// import 'dart:html';

import 'dart:convert';

import 'package:chewie/chewie.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hotwork/bean/GetCheckQuestionByIdBean.dart';
import 'package:flutter_hotwork/homepage/BaseTitleBar.dart';
import 'package:flutter_hotwork/homepage/BaseViewBar.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';
import 'package:flutter_hotwork/xunjian/weight/image_glide_widget.dart';
import 'package:flutter_hotwork/xunjian/weight/video_play_widget.dart';
import 'package:flutter_hotwork/xunjian/weight/voice_player_view.dart';
import 'package:flutter_plugin_record/flutter_plugin_record.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:video_player/video_player.dart';

import '../Toast.dart';

// FlutterSound flutterSound =  FlutterSound();
class XunJianWenTiShangBaoDetails extends StatefulWidget {
  int id;

  XunJianWenTiShangBaoDetails(this.id);

  @override
  XunJianEditState2 createState() => XunJianEditState2();
}

GetCheckQuestionByIdBean getCheckQuestionByIdBean;
List<Widget> imageviews = List();
String imgs = "";
String videos = "";
String voices = "";
//用户类型的控制器
TextEditingController userController = TextEditingController();
VideoPlayerController videoPlayerController = VideoPlayerController.asset('');
ChewieController chewieController = ChewieController(
  videoPlayerController: videoPlayerController,
  aspectRatio: 1,
  autoPlay: true,
  looping: true,
);
final playerWidget = Chewie(
  controller: chewieController,
);

class XunJianEditState2 extends State<XunJianWenTiShangBaoDetails> {
  List voiceUrl; //音频
  List videoUrl; //视频
  List sitePhotosUrl; //图片

  //控制器
  TextEditingController inputController = TextEditingController();
  FlutterPluginRecord recordPlugin = FlutterPluginRecord();
  bool imagesUpload = true;
  var _value;
  var index = -1;

  @override
  void initState() {
    super.initState();
    // 初始化
    recordPlugin.initRecordMp3();
    DioUtils.createInstance();
    getCheckQuestionById();
  }

  @override
  Widget build(BuildContext context) {
    double rpx = MediaQuery.of(context).size.width / 750;
    recordPlugin.response.listen((data) {
      if (data.msg == "onStop") {
        ///结束录制时会返回录制文件的地址方便上传服务器

        print("onStop " + data.path);
        LocalToast.toast(context, msg: "录音已结束");
      } else if (data.msg == "onStart") {
        print("onStart --");
        LocalToast.toast(context, msg: "录音已开始", showTime: 2000);
      }
    });

    return Scaffold(
        appBar: BaseViewBar(
            childView: BaseTitleBar(
              "详情",
              leftIcon: 'images/fanhui.png',
              rightIcon: "",
              rightClick: () {
                print("点击了右边干嘛啊。。。哦");
              },
              leftClick: () {
                Navigator.of(context).pop();
                print("点击了左边干嘛啊。。。哦");
              },
            ),
            preferredSize: Size.fromHeight(50.0)),
        body: CustomScrollView(
          shrinkWrap: true,
          // 内容
          slivers: <Widget>[
            SliverPadding(
              padding: const EdgeInsets.all(0),
              sliver: SliverList(
                delegate: getCheckQuestionByIdBean == null
                    ? SliverChildListDelegate(<Widget>[Container()])
                    : SliverChildListDelegate(
                        <Widget>[
                          Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5)),
                              boxShadow: <BoxShadow>[
                                //设置阴影
                                BoxShadow(
                                  color: Colors.grey, //阴影颜色
                                  // blurRadius:5.0,//阴影大小
                                ),
                              ],
                            ),
                            margin: EdgeInsets.all(10),
                            padding: EdgeInsets.all(10),
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text("上报时间:"),
                                    Flexible(
                                        fit: FlexFit.tight, child: SizedBox()),
                                    Text(getCheckQuestionByIdBean
                                        .result.createTime),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text("人员姓名:"),
                                    Flexible(
                                        fit: FlexFit.tight, child: SizedBox()),
                                    Text(getCheckQuestionByIdBean
                                        .result.checkUserName),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text("区域名称:"),
                                    Flexible(
                                        fit: FlexFit.tight, child: SizedBox()),
                                    Text(getCheckQuestionByIdBean
                                        .result.areaName),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text("设备名称:"),
                                    Flexible(
                                        fit: FlexFit.tight, child: SizedBox()),
                                    Text(getCheckQuestionByIdBean
                                        .result.deviceName),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text("问题项目:"),
                                    Flexible(
                                        fit: FlexFit.tight, child: SizedBox()),
                                    Text(getCheckQuestionByIdBean
                                        .result.projectName),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text("问题地点:"),
                                    Flexible(
                                        fit: FlexFit.tight, child: SizedBox()),
                                    Text(getCheckQuestionByIdBean
                                                .result.questionAddress !=
                                            null
                                        ? getCheckQuestionByIdBean
                                            .result.questionAddress
                                        : ""),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text("任务名称:"),
                                    Flexible(
                                        fit: FlexFit.tight, child: SizedBox()),
                                    Text(getCheckQuestionByIdBean
                                        .result.checkName),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text("项目结果:"),
                                    Flexible(
                                        fit: FlexFit.tight, child: SizedBox()),
                                    Text(getCheckQuestionByIdBean
                                        .result.statusStr),
                                  ],
                                ),
                              ],
                            ),
                          ),
                          Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5)),
                              boxShadow: <BoxShadow>[
                                //设置阴影
                                BoxShadow(
                                  color: Colors.grey, //阴影颜色
                                  // blurRadius:5.0,//阴影大小
                                ),
                              ],
                            ),
                            margin: EdgeInsets.only(
                                left: 10, right: 10, bottom: 10),
                            padding: EdgeInsets.all(10),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text("问题描述", style: TextStyle(fontSize: 16)),
                                Container(
                                  padding: EdgeInsets.all(10),
                                  child: Row(
                                    children: [
                                      // Image(height:20,width: 20,image: AssetImage('images/mingcheng.png')),
                                      Expanded(
                                        child: Text(
                                          getCheckQuestionByIdBean
                                              .result.questionDescription,
                                          maxLines: 100,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Text("图片文件", style: TextStyle(fontSize: 16)),
                                getCheckQuestionByIdBean.result.sitePhotosUrl != null &&
                                        getCheckQuestionByIdBean.result.sitePhotosUrl.length > 0
                                    ? ImageGlideWidget(
                                        list: sitePhotosUrl)
                                    : Text(""),
                                Text("视频文件", style: TextStyle(fontSize: 16)),
                                getCheckQuestionByIdBean.result.videoUrl != null &&
                                        getCheckQuestionByIdBean.result.videoUrl.length > 0
                                    ? videoUrl != null && videoUrl.length > 0
                                        ? ListView.builder(
                                            shrinkWrap: true,
                                            itemCount: videoUrl==null?0:videoUrl.length,
                                            itemBuilder: (BuildContext context,
                                                int index) {
                                              return Container(
                                                height: 100,
                                                child: VideoPlayWidget(
                                                    videoPlayerController:
                                                        VideoPlayerController
                                                            .network(DioUtils.API_PREFIX +videoUrl[
                                                                index])),
                                              );
                                            })
                                        : Text("")
                                    : Text(""),
                                Text("音频文件", style: TextStyle(fontSize: 16)),
                                getCheckQuestionByIdBean.result.voiceUrl != null &&
                                        getCheckQuestionByIdBean.result.voiceUrl.length > 0
                                    ? Text(
                                        "共上传${voiceUrl.length}条录音",
                                      )
                                    : Text(""),
                                getCheckQuestionByIdBean.result.voiceUrl != null &&
                                        getCheckQuestionByIdBean.result.voiceUrl.length > 0
                                    ? VoicePlayerView(DioUtils.API_PREFIX +voiceUrl[voiceUrl.length - 1], new FlutterSound())
                                    : Text(""),
                              ],
                            ),
                          ),
                        ],
                      ),
              ),
            ),
          ],
        ));
  }

  @override
  void dispose() {
    imgs = "";
    videos = "";
    voices = "";
    userController..text = "";
    videoPlayerController.pause();
    chewieController.pause();
    recordPlugin.dispose();
    super.dispose();
  }

//获取详情
  void getCheckQuestionById() async {
    // Navigator.pop(context);
    try {
      Response response;
      response = await DioUtils.dio.get(DioUtils.API_PREFIX +
          "/checkQuestion/getCheckQuestionById?id=" +
          widget.id.toString());
      Map respMap = json.decode(response.data);
      getCheckQuestionByIdBean = GetCheckQuestionByIdBean.fromJson(respMap);
      if(getCheckQuestionByIdBean!=null&&getCheckQuestionByIdBean.result!=null){
        if(getCheckQuestionByIdBean.result.sitePhotosUrl!=null&&getCheckQuestionByIdBean.result.sitePhotosUrl.length>0){
          String s = getCheckQuestionByIdBean.result.sitePhotosUrl.substring(0,getCheckQuestionByIdBean.result.sitePhotosUrl.length - 1);
          sitePhotosUrl = s.split(",");
        }
        if(getCheckQuestionByIdBean.result.videoUrl!=null&&getCheckQuestionByIdBean.result.videoUrl.length>0){
          String s = getCheckQuestionByIdBean.result.videoUrl.substring(0,getCheckQuestionByIdBean.result.videoUrl.length - 1);
          videoUrl = s.split(",");
        }
        if(getCheckQuestionByIdBean.result.voiceUrl!=null&&getCheckQuestionByIdBean.result.voiceUrl.length>0){
          String s = getCheckQuestionByIdBean.result.voiceUrl.substring(0,getCheckQuestionByIdBean.result.voiceUrl.length - 1);
          voiceUrl = s.split(",");
        }
      }
      setState(() {});
    } catch (e) {
      print('请求出错：' + e.toString());
    }
  }
}
